package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.demo.adapter.JokeAdapter;
import com.example.demo.api.ApiService;
import com.example.demo.entity.Joke;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitDemoActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private Context context;
    private JokeAdapter adapter;
    private TextView tvTittle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrofit_demo);

        recyclerView = findViewById(R.id.recycler_view);
        tvTittle = findViewById(R.id.toolbar_title);
        tvTittle.setText("笑话");

        recyclerView.setLayoutManager( new LinearLayoutManager(context));
        adapter = new JokeAdapter();
        recyclerView.setAdapter(adapter);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/api/")
                //.addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiService api = retrofit.create(ApiService.class);

        Call<ResponseBody> call = api.getJokes(20);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
            {
                ResponseBody responseBody = response.body();
                try
                {
                    String jsonStr = responseBody.string();
                    Log.e("jsonStr", jsonStr);
                    Gson gson = new Gson();
                    Joke joke = gson.fromJson(jsonStr, Joke.class);
                    adapter.setData(joke.getJokes());
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t)
            {
                Log.e("onFailure", t.getMessage());
                t.printStackTrace();
            }
        });

//        Call<ResponseBody> call = api.listRepos("octocat");
//
//
//        //同步请求
//        //call.execute();
//
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                //请求成功
//                //List<Repo> repos = response.body();
//                Log.e("onResponse", response.toString());
//
//
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                //请求失败
//                Log.e("onFailure" , t.getMessage());
//                t.printStackTrace();
//            }
//        });
    }
}